﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

namespace PRO.Controls
{
    public class ValidateCode : IValidateCodeStrategy
    {
        
        protected int _ValidateCodeType = 0;
        /// <summary>
        /// 验证码字符类型  0：纯数字；1：纯字母；2：数字加字母
        /// </summary>
        public int ValidateCodeType
        {
            get { return _ValidateCodeType; }
            set { _ValidateCodeType = value; }
        }

        
        protected int _Length = 4;
        /// <summary>
        /// 字符长度
        /// </summary>
        public int Length
        {
            get { return _Length; }
            set { _Length = value; }
        }
        protected int _SingleStrWidth = 17;
        /// <summary>
        /// 单个字符宽度范围
        /// </summary>
        public int SingleStrWidth
        {
            get { return _SingleStrWidth; }
            set { _SingleStrWidth = value; }
        }
        protected int _SingleStrHeight = 20;
        /// <summary>
        /// 单个字符高度范围
        /// </summary>
        public int SingleStrHeight
        {
            get { return _SingleStrHeight; }
            set { _SingleStrHeight = value; }
        }

        protected Font _Font = new Font("Arial", 9, FontStyle.Regular);
        /// <summary>
        /// 文字字体
        /// </summary>
        public Font Font
        {
            get { return _Font; }
            set { _Font = value; }
        }
        protected Color _FontColor = Color.Black;
        /// <summary>
        /// 文字颜色
        /// </summary>
        public Color FontColor
        {
            get { return _FontColor; }
            set { _FontColor = value; }
        }
        protected Color _BackgroundColor = Color.White;
        /// <summary>
        /// 图片背景颜色
        /// </summary>
        public Color BackgroundColor
        {
            get { return _BackgroundColor; }
            set { _BackgroundColor = value; }
        }

        public void Create()
        {
            string checkCode = GetRandomStr();
            HttpContext.Current.Session["HHC_ValidateCode"] = checkCode;
            int int_ImageWidth = checkCode.Length * SingleStrWidth+3;
            Random newRandom = new Random();
            Bitmap image = new Bitmap(int_ImageWidth, SingleStrHeight+3);
            Graphics g = Graphics.FromImage(image);
            //生成随机生成器
            Random random = new Random();
            //白色背景
            g.Clear(BackgroundColor);
            

            for (int int_index = 0; int_index < checkCode.Length; int_index++)
            {
                string str_char = checkCode.Substring(int_index, 1);
                Brush newBrush = new SolidBrush(FontColor);
                Point thePos = new Point(int_index * SingleStrWidth + 1 + newRandom.Next(3), 1 + newRandom.Next(3));
                g.DrawString(str_char, this.Font, newBrush, thePos);
            }
            //将生成的图片发回客户端
            MemoryStream ms = new MemoryStream();
            image.Save(ms, ImageFormat.Png);
            System.Web.HttpContext.Current.Response.ClearContent(); //需要输出图象信息 要修改HTTP头 
            System.Web.HttpContext.Current.Response.ContentType = "image/Png";
            System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray());
            g.Dispose();
            image.Dispose();
            System.Web.HttpContext.Current.Response.End();
        }
        protected string GetRandomStr()
        {
            string str = null;
            char[] RandomStr = null;
            switch (ValidateCodeType)
            {
                case 0:
                    RandomStr = "0123456789".ToCharArray();
                    break;
                case 1:
                    RandomStr = "ABCDEFGHJKMNPQRSTUVWXYZ".ToCharArray();
                    break;
                case 2:
                    RandomStr = "0123456789ABCDEFGHJKMNPQRSTUVWXYZ".ToCharArray();
                    break;
            }
            System.Random random = new Random((int)DateTime.Now.Ticks);
            string validateCode = string.Empty;
            for (int i = 0; i < Length; i++)
                str += RandomStr[random.Next(0, RandomStr.Length)].ToString();
            return str;
        }
    }
}
